home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1995 November / Macworld Nov ’95.toast / Developers / ResAnomaly 1.1 / ResAnomaly Source / LTempHandleStream.cp < prev    next >
Encoding:
Text File  |  1995-06-24  |  3.1 KB  |  150 lines  |  [TEXT/MPCC]

  1. // ===========================================================================
  2. //    LHandleStream.cp                ©1993 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    A Stream whose bytes are in a Handle block in memory
  6.  
  7. #ifdef PowerPlant_PCH
  8. #include PowerPlant_PCH
  9. #endif
  10.  
  11. #include <LHandleStream.h>
  12.  
  13.  
  14. // ---------------------------------------------------------------------------
  15. //        • LHandleStream
  16. // ---------------------------------------------------------------------------
  17. //    Default Constructor
  18.  
  19. LHandleStream::LHandleStream()
  20. {
  21.     OSErr    theErr;
  22.     
  23.     mDataH = ::TempNewHandle(0, &theErr);            // Start with a zero length Handle
  24.     // * purposely ignoring theErr
  25.     
  26.     ThrowIfMemFail_(mDataH);
  27. }
  28.  
  29.  
  30. // ---------------------------------------------------------------------------
  31. //        • LHandleStream(Handle)
  32. // ---------------------------------------------------------------------------
  33. //    Construct from an existing Handle
  34. //
  35. //    The LHandleStream object assumes control of the Handle
  36.  
  37. LHandleStream::LHandleStream(
  38.     Handle    inHandle)
  39. {
  40.     mDataH = inHandle;
  41.     LStream::SetLength(GetHandleSize(inHandle));
  42. }
  43.  
  44.  
  45. // ---------------------------------------------------------------------------
  46. //        • ~LHandleStream
  47. // ---------------------------------------------------------------------------
  48. //    Destructor
  49.  
  50. LHandleStream::~LHandleStream()
  51. {
  52.     if (mDataH != nil) {
  53.         ::DisposeHandle(mDataH);
  54.     }
  55. }
  56.  
  57.  
  58. void
  59. LHandleStream::SetLength(
  60.     Int32    inLength)
  61. {
  62.     ::SetHandleSize(mDataH, inLength);
  63.     ThrowIfMemError_();
  64.     LStream::SetLength(inLength);
  65. }
  66.  
  67.  
  68. Int32
  69. LHandleStream::WriteData(
  70.     const void    *inBuffer,
  71.     Int32        inByteCount)
  72. {
  73.     Int32    bytesWritten = inByteCount;
  74.     Int32    endOfWrite = GetMarker() + inByteCount;
  75.     if (endOfWrite > GetLength()) {
  76.                                     // Need to grow Handle
  77.         Try_ {
  78.             SetLength(endOfWrite);
  79.         }
  80.         
  81.         Catch_(inErr) {                // Grow failed. Write only what fits.
  82.             bytesWritten = GetLength() - GetMarker();
  83.         } EndCatch_
  84.     }
  85.  
  86.                                     // Copy bytes into Handle
  87.     ::BlockMoveData(inBuffer, *mDataH + GetMarker(), bytesWritten);
  88.     SetMarker(bytesWritten, streamFrom_Marker);
  89.     return bytesWritten;
  90. }
  91.  
  92.  
  93. Int32
  94. LHandleStream::ReadData(
  95.     void    *outBuffer,
  96.     Int32    inByteCount)
  97. {
  98.                                     // Upper bound is number of bytes from
  99.                                     //   marker to end
  100.     Int32    bytesRead = inByteCount;
  101.     if ((GetMarker() + bytesRead) > GetLength()) {
  102.         bytesRead = GetLength() - GetMarker();
  103.     }
  104.                                     // Copy bytes from Handle into buffer
  105.     ::BlockMoveData(*mDataH + GetMarker(), outBuffer, bytesRead);
  106.     SetMarker(bytesRead, streamFrom_Marker);
  107.  
  108.     return bytesRead;
  109. }
  110.  
  111.  
  112. void
  113. LHandleStream::SetDataHandle(
  114.     Handle    inHandle)
  115. {
  116.     if (mDataH != nil) {
  117.         ::DisposeHandle(mDataH);
  118.     }
  119.         
  120.     mDataH = inHandle;
  121.     
  122.     Int32    newLength = 0;
  123.     if (inHandle != nil) {
  124.         newLength = GetHandleSize(inHandle);
  125.     }
  126.     LStream::SetLength(newLength);
  127.         
  128.     SetMarker(0, streamFrom_Start);
  129. }
  130.  
  131.  
  132. Handle
  133. LHandleStream::GetDataHandle()
  134. {
  135.     return mDataH;
  136. }
  137.  
  138.  
  139. Handle
  140. LHandleStream::DetachDataHandle()
  141. {
  142.     Handle    dataHandle = mDataH;    // Save current data Handle
  143.     
  144.     LStream::SetLength(0);
  145.     mDataH = ::NewHandle(0);        // Reset to zero-sized Handle
  146.     ThrowIfMemFail_(mDataH);
  147.     
  148.     return dataHandle;
  149. }
  150.